﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary> 单张图片的幻影效果组件 </summary>  
public class GhostEffect : MonoBehaviour
{
    /// <summary> 幻影所需要复制的游戏运行时的对象，需要有SpriteRenderer </summary>  
    public GameObject CopySpriteGameObject;
    /// <summary> 幻影的父对象 </summary>  
    public Transform GhostParent;
    /// <summary> 间隔时间 </summary>  
    public float Rate = 0.1f;
    /// <summary> 幻影总数 </summary>  
    public int GhostMaxCount = 3;

    public bool AblityPermitted = false;

    //  计时用的
    private float timer = 0;
    /// <summary> 幻影的对象池 </summary>  
    [SerializeField]
    private List<GameObject> _GhostPool = new List<GameObject>();

    void Start()
    {
        if (null == GhostParent)
            GhostParent = GameObject.Find("/Others/GhostParent").transform;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            StartGhost();
        }
        if (Input.GetKeyDown(KeyCode.Y))
        {
            StopGhost();
        }
        if (AblityPermitted)
        {
            if (timer < Time.time)
            {
                timer = Time.time + Rate;

                OpenGhost();
            }
        }
    }

    private void OpenGhost()
    {
        GameObject go = null;

        go = CreateGhost();

        List<Renderer> lRenderers = new List<Renderer>(go.GetComponentsInChildren<Renderer>());

        go.transform.position = CopySpriteGameObject.transform.position;

        StartCoroutine(GhostChange(Rate * GhostMaxCount, go.gameObject));
    }

    private GameObject CreateGhost()
    {
        GameObject go = null;
        if (_GhostPool.Count > 1)
        {
            go = _GhostPool[0];
            go.SetActive(true);
            _GhostPool.RemoveAt(0);

            //  此时是对象池里面取得，图片不同，手动改图 改scale
            go.GetComponent<SpriteRenderer>().sprite = CopySpriteGameObject.GetComponent<SpriteRenderer>().sprite;
            go.transform.localScale = CopySpriteGameObject.transform.lossyScale;
        }
        else
        {
            go = GameObject.Instantiate(CopySpriteGameObject);
            //  只保留go本身，此组件由于对象池取出时子对象的赋图麻烦   所以暂不考虑子对象
            for (int i = 0; i < go.transform.childCount; i++)
            {
                Destroy(go.transform.GetChild(i).gameObject);
            }

            if (null != GhostParent)
                go.transform.SetParent(GhostParent);

            Animator[] animator = go.GetComponentsInChildren<Animator>();   //  这个方法包括自身
            foreach (var item in animator)
                item.enabled = false;

            //  6.24 TODO !!!  由于这个版本的动画  collider和动画绑定了  所以我无法把sprite独立  于是还要禁用collider
            Collider[] colliders = go.GetComponentsInChildren<Collider>();
            foreach (var item in colliders)
                item.enabled = false;
        }

        return go;
    }

    // 开始残影
    public void StartGhost()
    {
        AblityPermitted = true;
    }

    // 停止残影
    public void StopGhost()
    {
        AblityPermitted = false;
    }

    IEnumerator GhostChange(float duaration, GameObject go)
    {
        float time = 0;
        while (time < duaration)
        {
            time += Time.deltaTime;
            float a = Mathf.Lerp(100f / 255, 0, time / duaration);
            List<SpriteRenderer> renderers = new List<SpriteRenderer>(go.GetComponentsInChildren<SpriteRenderer>());
            renderers.ForEach(sr => sr.color = new Color(0, 0, 0, a));
            yield return new WaitForEndOfFrame();
        }
        DestroyGhost(go);
    }
    private void DestroyGhost(GameObject go)
    {
        if (_GhostPool.Count <= GhostMaxCount + 3)
        {
            go.SetActive(false);
            _GhostPool.Add(go);
        }
        else
            Destroy(go);
    }

}
